package st.business.tepth.business.user.busnumber.workholder.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.R;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.CommonBean;
import st.business.tepth.business.bean.MaintainNotWorkWrapper;

/**
 * @author zhangyu
 * @date 2019/5/17
 */
public class DialogMaintainNotWork extends Dialog {
    private TextView mTvConfirm;
    private List<MaintainNotWorkWrapper> mDataList;

    public DialogMaintainNotWork(@NonNull Context context,
                                 final List<MaintainNotWorkWrapper> list,
                                 final OnItemChooseListener<String> listener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDataList = list;
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_maintain_not_work, null);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view_work);

        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mTvConfirm = view.findViewById(R.id.tv_confirm);
        mTvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onChoose(convertPostData(list));
                }
                dismiss();
            }
        });
        final MyAdapter myAdapter;
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(myAdapter = new MyAdapter(R.layout.item_maintain_not_work,
                list,
                context));
        setContentView(view);
        setEnable(false);
        RadioGroup radioGroup = view.findViewById(R.id.radio_group_all_reason);
        if (list != null && !list.isEmpty()) {
            List<CommonBean> commonBeans = list.get(0).getResonList();
            if (commonBeans != null) {
                for (int i = 0, size = commonBeans.size(); i < size; i++) {
                    CommonBean commonBean = commonBeans.get(i);
                    RadioButton rb = (RadioButton) radioGroup.getChildAt(i);
                    if (rb == null) {
                        rb = (RadioButton) LayoutInflater.from(context)
                                .inflate(R.layout.item_maintain_not_work_reason,
                                        null);
                        radioGroup.addView(rb);
                    }
                    rb.setId(commonBean.getId());
                    rb.setText(commonBean.getName());
                }
            }
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    List<MaintainNotWorkWrapper> allList = myAdapter.getData();
                    if (!allList.isEmpty()) {
                        for (MaintainNotWorkWrapper maintainNotWorkWrapper :
                                allList) {
                            maintainNotWorkWrapper.setCheckedReasonId(checkedId);
                        }
                        myAdapter.notifyDataSetChanged();
                    }
                    setEnable(isAllChoose());
                }
            });
        }
    }

    public class MyAdapter extends
            BaseQuickAdapter<MaintainNotWorkWrapper, BaseViewHolder> {
        private Context mContext;

        public MyAdapter(int layoutResId,
                         @Nullable List<MaintainNotWorkWrapper> data,
                         Context context) {
            super(layoutResId, data);
            mContext = context;
        }

        @Override
        protected void convert(BaseViewHolder helper, final MaintainNotWorkWrapper item) {
            helper.setText(R.id.tv_name, item.getName());
            RadioGroup radioGroup = helper.getView(R.id.radio_group);
            radioGroup.setOnCheckedChangeListener(null);
            List<CommonBean> commonBeans = item.getResonList();
            for (int i = 0, size = commonBeans.size(); i < size; i++) {
                CommonBean commonBean = commonBeans.get(i);
                RadioButton rb = (RadioButton) radioGroup.getChildAt(i);
                if (rb == null) {
                    rb = (RadioButton) LayoutInflater.from(mContext)
                            .inflate(R.layout.item_maintain_not_work_reason,
                                    null);
                    radioGroup.addView(rb);
                }
                rb.setId(commonBean.getId());
                rb.setText(commonBean.getName());
            }
            radioGroup.check(item.getCheckedReasonId());
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    item.setCheckedReasonId(checkedId);
                    setEnable(isAllChoose());
                }
            });
        }
    }

    private void setEnable(boolean isEnable) {
        mTvConfirm.setBackgroundResource(isEnable ? R.drawable.shape_btn_operation_n :
                R.drawable.shape_btn_operation_t);
        mTvConfirm.setTextColor(isEnable ? Color.parseColor("#333333") :
                Color.parseColor("#ffffff"));
        mTvConfirm.setEnabled(isEnable);
    }

    private boolean isAllChoose() {
        if (mDataList != null) {
            for (MaintainNotWorkWrapper workWrapper : mDataList) {
                if (workWrapper.getCheckedReasonId() == 0) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    private String convertPostData(List<MaintainNotWorkWrapper> list) {
        List<PostData> postDataList = new ArrayList<>();
        for (MaintainNotWorkWrapper workWrapper : list) {
            postDataList.add(new PostData(workWrapper.getId(), workWrapper.getCheckedReasonId()));
        }
        return JSON.toJSONString(postDataList);
    }

    private class PostData {
        private int id;
        private int reasonCode;

        public PostData() {
        }

        public PostData(int id, int reasonCode) {
            this.id = id;
            this.reasonCode = reasonCode;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getReasonCode() {
            return reasonCode;
        }

        public void setReasonCode(int reasonCode) {
            this.reasonCode = reasonCode;
        }
    }
}
